/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
/*
 * char_send.c
 * Copyright (C) Francesco Marcantoni 2006 <ramyel@gmail.com>
 * 
 * You may redistribute it and/or modify it under the terms of the
 * GNU General Public License, as published by the Free Software
 * Foundation; either version 2 of the License, or (at your option)
 * any later version.
 * 
 * main.c is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with main.c.  If not, write to:
 * 	The Free Software Foundation, Inc.,
 * 	51 Franklin Street, Fifth Floor
 * 	Boston, MA  02110-1301, USA.
 */

#include "../include/tokyo.h"

void char_d_send (S_DESCRIPTOR *d, NetCType type, ... )
{
	gchar buf [MSL];
	gchar bufNetCType[4];
	gboolean is_type_param = TRUE;
	SendType type_param = SEND_NUMBER;
	gchar *bufsend;
	va_list param;
	gint leng = 0;

	g_return_if_fail (type < NET_CTYPE_MAX);
	g_return_if_fail (d != NULL);


	// Compongo il Buffer
	va_start (param, type);

	while (1)
	{
		if (is_type_param)
		{
			type_param = va_arg (param, gint);
			is_type_param = FALSE;
		}
		else
		{
			is_type_param = TRUE;

			switch (type_param)
			{
				default:
					tlog (P_ERROR, "char_d_send", _("Send to %d, type_param non valido %d"), d, type_param);
					return;
				break;

				case SEND_STOP:
					// Aggiungo come primo elemento del buffer in uscita il
					// codice relativo al tipo di dati inviati.
					buf[leng-1] = '\0';
					g_snprintf (bufNetCType, 4, "%d ", type);
					bufsend = g_strconcat (bufNetCType, buf, "\n\r", NULL);

					net_write_to_buffer (d, bufsend, strlen(bufsend));

					g_free (bufsend);
					return;
				break;

				case SEND_NUMBER:
					g_snprintf (buf+leng, MSL-leng, "%d ", va_arg (param, gint));
					leng += strlen(buf+leng);
				break;

				case SEND_CHAR:
					g_snprintf (buf+leng, MSL-leng, "%c ", va_arg (param, gint));
					leng += strlen(buf+leng);
				break;

				case SEND_STRING:
					g_snprintf (buf+leng, MSL-leng, "$%s$ ", va_arg (param, gchar*));
					leng += strlen(buf+leng);
				break;
			}
		}
	}

	va_end  (param);
}

void char_send (S_CHAR *ch, NetCType type, ... )
{
	gchar buf [MSL];
	gchar bufNetCType[4];
	gchar *bufsend;
	gboolean is_type_param = TRUE;
	SendType type_param = SEND_NUMBER;
	va_list param;
	gint leng = 0;

	g_return_if_fail (type < NET_CTYPE_MAX);
	g_return_if_fail (ch != NULL);

	if (!ch->desc)
	{
		tlog (P_ERROR, "char_send", _("Send to %s, descrittore nullo"), ch->name);
		return;
	}

	// Compongo il Buffer
	va_start (param, type);

	while (1)
	{
		if (is_type_param)
		{
			type_param = va_arg (param, gint);
			is_type_param = FALSE;
		}
		else
		{
			is_type_param = TRUE;

			switch (type_param)
			{
				default:
					tlog (P_ERROR, "char_send", _("Send to %s, type_param non valido %d"), ch->name, type_param);
					return;
				break;

				case SEND_STOP:
					// Aggiungo come primo elemento del buffer in uscita il
					// codice relativo al tipo di dati inviati.
					buf[leng-1] = '\0';
					g_snprintf (bufNetCType, 4, "%d ", type);
					bufsend = g_strconcat (bufNetCType, buf, "\n\r", NULL);

					net_write_to_buffer (ch->desc, bufsend, strlen(bufsend));

					g_free (bufsend);
					return;
				break;

				case SEND_NUMBER:
					g_snprintf (buf+leng, MSL-leng, "%d ", va_arg (param, gint));
					leng += strlen(buf+leng);
				break;

				case SEND_CHAR:
					g_snprintf (buf+leng, MSL-leng, "%c ", va_arg (param, gint));
					leng += strlen(buf+leng);
				break;

				case SEND_STRING:
					g_snprintf (buf+leng, MSL-leng, "$%s$ ", va_arg (param, gchar*));
					leng += strlen(buf+leng);
				break;
			}
		}
	}

	va_end  (param);
}

void char_send_prompt (S_CHAR *ch)
{
	g_return_if_fail (ch != NULL);

	/* Invio tutte le informazioni utili per comporre il prompt */
	char_send (ch, NET_CTYPE_PROMPT, SEND_STRING, "Prompt", SEND_NUMBER, 234, SEND_CHAR, 'f', SEND_STRING, "Oh yea!", SEND_STOP);
}
